export default class History{
    constructor (router) {
        this.router = router;
        // 在路由中保存一个当前的路径，后续的路径变化均在这个上面操作
        this.current = createRoute(null, {
            path: '/',
            matched: []
        });
    }

    // 跳转的核心逻辑， location 代表要去的路由地址， onComplete 表示跳转成功后要执行的方法
    transitionTo (location, onComplete) {
        let route = this.router.match(location);
        console.log(route);
        onComplete && onComplete();
    }
};

export function createRoute (record, location) {
    let res = [];
    if (record) {
        while (record) {
            res.unshift(record.path);
            record = record.parent;
        }
    }
    return {
        ...location,
        matched: res
    }
};
